package com.kylin.androidlearn1.acticity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import com.kylin.androidlearn1.Presentation.DemoPresentation;
import com.kylin.androidlearn1.R;

import java.io.IOException;

/**
 * Created by kylinhuang on 10/6/16.
 */
public class MusicPlayAtcicity extends Activity implements View.OnClickListener {
    private MediaPlayer mediaPlayer;
    private MediaPlayer.OnBufferingUpdateListener mOnBufferingUpdateListener = new MediaPlayer.OnBufferingUpdateListener() {
        @Override
        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            mSeekBar.setProgress(percent);
        }
    };
    private MediaPlayer.OnPreparedListener mOnPreparedListener = new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mSeekBar.setProgress(mp.getDuration());
            mp.start();
        }
    };
    private SeekBar mSeekBar;
    private Button mBtnPlay;
    private Button mBtnPause;
    private Button mBtnStop;
//    private String playurl = "http://219.238.7.72/files/4250000008BB2F59/mp3.446655.com/OOxx/wma/chb";
    private String playurl = "http://abv.cn/music/光辉岁月.mp3";
    private DisplayManager mDisplayManager;
    private Button mBtnPresentation;
    private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            //TODO  手动拖动到 某位置   -- 播放器 跳转至某位置开始播放
            mediaPlayer.seekTo(progress);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_play);

        initView();


        //初始化播放器
        initplay();

    }

    private String getDisplayCategory() {

        return null;
    }

    private void initView() {
        mSeekBar = (SeekBar)findViewById(R.id.MusicPlayAtcicity_seekbar);
        mSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangeListener);

        mBtnPlay = (Button)findViewById(R.id.MusicPlayAtcicity_play);
        mBtnPause = (Button)findViewById(R.id.MusicPlayAtcicity_pause);
        mBtnStop = (Button)findViewById(R.id.MusicPlayAtcicity_stop);


        mBtnPresentation = (Button)findViewById(R.id.MusicPlayAtcicity_presentation);

        mBtnPlay.setOnClickListener(this);
        mBtnPause.setOnClickListener(this);
        mBtnStop.setOnClickListener(this);

        mBtnPresentation.setOnClickListener(this);




    }

    /**
     * 初始化播放器
     */
    private void initplay() {
        mediaPlayer = new MediaPlayer(); //创建 播放器
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);// 设置媒体流类型
        mediaPlayer.setOnBufferingUpdateListener(mOnBufferingUpdateListener);  //设置播放更新监听 获取播放时间
        mediaPlayer.setOnPreparedListener(mOnPreparedListener); // 设置准备监听  开始播放 准备完成调用
    }

    public static void actionStart(Context mContext) {
        Intent intent =  new Intent();
        intent.setClass(mContext,MusicPlayAtcicity.class);
        mContext.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.MusicPlayAtcicity_play:
                play();
                break;
            case R.id.MusicPlayAtcicity_pause:
                pause();
                break;
            case R.id.MusicPlayAtcicity_stop:
                stop();
                break;
            case R.id.MusicPlayAtcicity_presentation:
                presentationShow();
                break;


        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void presentationShow() {
        //分屏显示
        mDisplayManager = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE);

        String displayCategory = DisplayManager.DISPLAY_CATEGORY_PRESENTATION;
        Display[] displays = mDisplayManager.getDisplays(displayCategory); //获取 所有的显示



        DemoPresentation presentation =  new DemoPresentation(this,displays[0]); //创建 自定义 Presentation
        presentation.show(); //显示
    }

    /**
     *
     * 播放
     */
    private void play() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(playurl); // 设置数据源
                    mediaPlayer.prepare(); // prepare自动播放
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }


    // 暂停
    public void pause() {
        mediaPlayer.pause();
    }

    // 停止
    public void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}
